/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * login.java
 *
 * Created on 08/01/2010, 18:12:41
 */

package cparc;

import com.mysql.jdbc.Statement;
import java.sql.ResultSet;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import java.util.*;

/**
 *
 * @author Administrador
 */
public class login extends javax.swing.JFrame {

    /** Creates new form login */
    public login() {
        initComponents();
        DefaultComboBoxModel comboModel = new DefaultComboBoxModel();

        Statement stm = null;
        String result = null;
        String consulta = "SELECT USUARIO FROM CPARC.USUARIOS ORDER BY USUARIO ASC";

        try  {
        stm = (Statement) conexao.getConnection(true).createStatement();
        ResultSet rs = stm.executeQuery(consulta);

        while(rs.next()){

            comboModel.addElement(rs.getString("USUARIO"));
        }

        }
        catch(Exception e){
            String status = e.getMessage();
            System.out.println(status);
        }

        comboUsuario.setModel(comboModel);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btLogin = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        comboUsuario = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        tfSenha = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("CPARC - LOGIN");
        setResizable(false);

        btLogin.setText("LOGIN");
        btLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btLoginActionPerformed(evt);
            }
        });

        jLabel1.setText("Usuário");

        jLabel2.setText("Senha");

        tfSenha.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tfSenhaKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tfSenha, javax.swing.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE)
                    .addComponent(comboUsuario, 0, 145, Short.MAX_VALUE))
                .addGap(38, 38, 38))
            .addGroup(layout.createSequentialGroup()
                .addGap(98, 98, 98)
                .addComponent(btLogin)
                .addContainerGap(99, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(38, 38, 38)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(comboUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(tfSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jLabel1))
                .addGap(36, 36, 36)
                .addComponent(btLogin)
                .addContainerGap(28, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btLoginActionPerformed

        String usuario;
        String nome = null;
        String senha;
        String permissao = null;
        
        if (tfSenha.getText().isEmpty()){

            JOptionPane.showMessageDialog(rootPane, "Digite uma senha!", "Senha Inválida", JOptionPane.ERROR_MESSAGE);
            tfSenha.requestFocusInWindow();
            tfSenha.selectAll();
        }
        else{
        
            senha = tfSenha.getText();
            usuario = (String) comboUsuario.getSelectedItem();
            
            DefaultComboBoxModel comboModel = new DefaultComboBoxModel();

        Statement stm = null;
        String result = null;
        String consulta = "SELECT NOME,SENHA,PERMISSAO FROM CPARC.USUARIOS WHERE USUARIO = '" + usuario + "'";

        try  {
            stm = (Statement) conexao.getConnection(true).createStatement();
            ResultSet rs = stm.executeQuery(consulta);
            rs.next();
            senha = (rs.getString("SENHA"));
            nome = (rs.getString("NOME"));
            permissao = (rs.getString("PERMISSAO"));
        }
        catch(Exception e){
            String status = e.getMessage();
            System.out.println(status);
        }

        if(senha.equals(tfSenha.getText())){

                JOptionPane.showMessageDialog(rootPane, "Seção iniciada para:  " + nome, "Senha Aceita", JOptionPane.WARNING_MESSAGE);
                Home home = new Home(nome, permissao);
                home.setTitle("CPARC - Cadastro de População Animal de Rio Claro - Usuário Autenticado: " + nome + " - Permissão: " + permissao);
                home.setVisible(true);
                this.dispose();
            }
            else{

                JOptionPane.showMessageDialog(rootPane, "Senha Incorreta", "Senha inválida", JOptionPane.ERROR_MESSAGE);
                tfSenha.requestFocusInWindow();
                tfSenha.selectAll();
            }    
        }
    }//GEN-LAST:event_btLoginActionPerformed

    private void tfSenhaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfSenhaKeyPressed

        if(evt.getKeyCode() == 10){

            btLogin.doClick();

        }
    }//GEN-LAST:event_tfSenhaKeyPressed

    /**
    * @param args the command line arguments
    */
    
    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btLogin;
    private javax.swing.JComboBox comboUsuario;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPasswordField tfSenha;
    // End of variables declaration//GEN-END:variables

}
